package com.jacklin.controller;

import com.jacklin.DTO.OrderDTO;
import com.jacklin.enums.ResultEnum;
import com.jacklin.exception.MyException;
import com.jacklin.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @Author:JackLin
 * @Create 2019/4/1 11:24
 */
@Slf4j
@Controller
@RequestMapping("/seller/order")
public class SellOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单列表
     * @param page 第几页，从第一页开始
     * @param size  一页有多少条数据
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,//page参数没传默认为第一页
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        PageRequest request =  new PageRequest(page-1, size);   //要从第0页开始查
        Page<OrderDTO> orderDTOPage = orderService.findList(request);
        map.put("orderDTOPage", orderDTOPage);
        map.put("currentPage", page);
        map.put("size", size);

        return new ModelAndView("order/list",map);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){
        try {
                OrderDTO orderDTO = orderService.findOne(orderId);
                orderService.cancel(orderDTO);
        }catch (MyException e){
            log.error("【卖家端取消订单】发生异常{}",e);
            map.put("msg", e.getMessage());
            map.put("url", "/newstarsell/seller/order/list");

            return new ModelAndView("common/error",map);    //viewName:返回的模型视图名称，map是模型数据，如果没有数据，model Map可能是{@code null}。
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url", "/newstarsell/seller/order/list");
        return new ModelAndView("common/success",map);
    }

    /**
     * 订单性详情
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String,Object> map){

        OrderDTO orderDTO  = new OrderDTO();
        try {
            orderDTO  = orderService.findOne(orderId);
        } catch (MyException e){
            log.error("【卖家端查询订单详情】发生异常{}",e);
            map.put("msg", e.getMessage());
            map.put("url", "/newstarsell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("orderDTO", orderDTO);
        return new ModelAndView("order/detail",map);
    }

    /**
     * 完结订单
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/finish")
    public ModelAndView finished(@RequestParam("orderId") String orderId,
                                 Map<String,Object> map){
        try {
            OrderDTO orderDTO = orderService.findOne(orderId);
            try {
                orderService.finish(orderDTO);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }catch (MyException e){
            log.error("【卖家端完结订单】发生异常{}",e);
            map.put("msg", e.getMessage());
            map.put("url", "/newstarsell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        map.put("url", "/newstarsell/seller/order/list");
        return new ModelAndView("common/success",map);

    }

}
